package online.yysh.charging;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.Observer;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import online.yysh.charging.base.BaseActivity;
import online.yysh.charging.base.BaseFragment;
import online.yysh.charging.base.TabFragmentAdapter;
import online.yysh.charging.bean.Login;
import online.yysh.charging.fragment.AutoChargingFullFragment;
import online.yysh.charging.fragment.MoneyChargingFragment;
import online.yysh.charging.fragment.PowerChargingFragment;
import online.yysh.charging.fragment.TimeChargingFragment;
import online.yysh.charging.livedata.UserManager;

import java.util.ArrayList;
import java.util.List;

public class SelectChargingModeActivity extends BaseActivity {

    private TextView tv_device_id, tv_port, tv_my_money;
    private ImageView iv_show;
    private boolean showStatus = true;
    private String myMoney;
    private TabLayout tabLayout;
    private ViewPager vp;
    private List<BaseFragment> fragments = new ArrayList<>();
    private TabFragmentAdapter adapter;
    private String[] titles = new String[]{"自动充满", "按电量充", "按时间充", "按金额充"};

    private String chargingData;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_charging_mode;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_my_money = findViewById(R.id.tv_my_money);
        tv_device_id = findViewById(R.id.tv_device_id);
        iv_show = findViewById(R.id.iv_show);
        tv_port = findViewById(R.id.tv_port);
        tabLayout = findViewById(R.id.tl);
        vp = findViewById(R.id.vp_charging);
        for (String title : titles) {
            tabLayout.addTab(tabLayout.newTab().setText(title));
        }
        fragments.add(new AutoChargingFullFragment());
        fragments.add(new PowerChargingFragment());
        fragments.add(new TimeChargingFragment());
        fragments.add(new MoneyChargingFragment());
        adapter = new TabFragmentAdapter(this, fragments, titles, getSupportFragmentManager());
        vp.setAdapter(adapter);
        tabLayout.setupWithViewPager(vp);
        iv_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (showStatus){
                    tv_my_money.setText("余额：********");
                    showStatus = false;
                    iv_show.setBackgroundResource(R.mipmap.invisible);
                }else{
                    tv_my_money.setText(myMoney);
                    showStatus = true;
                    iv_show.setBackgroundResource(R.mipmap.visible);
                }
            }
        });
    }

    @Override
    protected void initData() {
        chargingData = getIntent().getStringExtra("chargingData");
        String[] data = chargingData.split("_");
        tv_device_id.setText("桩号：" + data[0]);
        tv_port.setText("枪号：" + data[1]);
        UserManager.getInstance().getLiveData().observe(this, new Observer<Login>() {
            @Override
            public void onChanged(Login login) {
                myMoney = "余额："+login.getMoney() + "元";
                tv_my_money.setText(myMoney);
            }
        });
    }

    public String getData(){
        return chargingData;
    }
}